﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using System.Data.OleDb;
using pcsse.App_Code.entity;

namespace pcsse.management
{
    public partial class import : System.Web.UI.Page
    {
        public String m_errorinfo = "";
        private String UpFileDir = "UpExcel/";
        private pcsse.App_Code.entity.import imp = new pcsse.App_Code.entity.import();
        protected FileUpload xlsFu;
        DataControlFieldCollection pdcfc;
        DataControlFieldCollection padcfc;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Priority"] != null)
            {
                int pri = Convert.ToInt32(Session["Priority"].ToString());
                if (pri <= 2)
                {
                    Response.Redirect("~/default.aspx");
                }
            }
            try
            {
                if (!Directory.Exists(Server.MapPath(UpFileDir)))
                {
                    Directory.CreateDirectory(Server.MapPath(UpFileDir));
                }
                if (!IsPostBack)
                {
                    if (!String.IsNullOrEmpty(Request.QueryString["filename"]))
                    {
                        importBt.Enabled = true;
                        imp.connToFile(Server.MapPath(UpFileDir) + Request.QueryString["filename"]);
                        detailsviewbinding(dataDdl.SelectedIndex, 0, "Sheet1$");
                        sheetDdl.DataSource = imp.GetTableList();
                        sheetDdl.DataBind();
                    }else{
                        foreach (string d in Directory.GetFileSystemEntries(Server.MapPath(UpFileDir)))
                        {
                            if (File.Exists(d))
                            {
                                File.Delete(d);
                            }
                        }
                        importBt.Enabled = false;
                    }
                }
                errLb.Text = "";
            }
            catch (System.Exception ex)
            {
                errLb.Text = ex.Message.ToString();
            }
        }

        protected void ufBt_Click(object sender, EventArgs e)
        {
            try
            {
                if (xlsFu.HasFile)
                {
                    string filename = System.DateTime.Now.ToFileTimeUtc() + xlsFu.FileName;
                    xlsFu.SaveAs(Server.MapPath(UpFileDir) + filename);
                    imp.connToFile(Server.MapPath(UpFileDir) + filename);
                    Response.Redirect("import.aspx?filename=" + filename);
                }else
                {
                    errLb.Text = "请选择文件";
                }
            }
            catch (System.Exception ex)
            {
                errLb.Text = ex.Message.ToString();
            }
        }

        protected void sheetDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (!String.IsNullOrEmpty(Request.QueryString["filename"]))
                {
                    imp.connToFile(Server.MapPath(UpFileDir) + Request.QueryString["filename"]);
                    detailsviewbinding(dataDdl.SelectedIndex, 0, sheetDdl.SelectedItem.Value);
                }
            }
            catch (System.Exception ex)
            {
                errLb.Text = ex.Message.ToString();
            }
        }

        protected void importBt_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["filename"]))
            {
                imp.connToFile(Server.MapPath(UpFileDir) + Request.QueryString["filename"]);
                detailsviewbinding(dataDdl.SelectedIndex, 1, sheetDdl.SelectedItem.Value);
            }
        }

        protected void dataDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["filename"]))
            {
                imp.connToFile(Server.MapPath(UpFileDir) + Request.QueryString["filename"]);
                detailsviewbinding(dataDdl.SelectedIndex, 0, sheetDdl.SelectedItem.Value);
            }
        }

        protected void detailsviewbinding(int mode, int method, string sheet)
        {
            if (mode == 0)
            {
                if (method == 0)
                {
                    pGv.DataSource = imp.GetData(sheet, 0);
                    pGv.Visible = true;
                    errGv.DataSource = null;
                    errGv.Visible = false;
                }else{
                    pGv.DataSource = null;
                    pGv.Visible = false;
                    errGv.DataSource = imp.DataImport(sheet, 0);
                    errGv.Visible = true;
                }
                partyGv.DataSource = null;
                partyGv.Visible = false;
            }
            else
            {
                if (method == 0)
                {
                    partyGv.DataSource = imp.GetData(sheet, 1);
                    partyGv.Visible = true;
                    errGv.DataSource = null;
                    errGv.Visible = false;
                }else
                {
                    partyGv.DataSource = null;
                    partyGv.Visible = false;
                    errGv.DataSource = imp.DataImport(sheet, Convert.ToInt32(branchDdl.SelectedItem.Value));
                    errGv.Visible = true;
                }
                pGv.DataSource = null;
                pGv.Visible = false;
            }
            errGv.DataBind();
            pGv.DataBind();
            partyGv.DataBind();
        }
    }
}
